/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.handler.notify;

import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.wx.api.WxPayService;
import com.egzosn.pay.wx.bean.WxPayMessage;
import com.medusa.gruul.common.core.config.Jackson;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.common.constant.WxConst;
import com.medusa.gruul.payment.common.enums.INotifyResponse;
import com.medusa.gruul.payment.common.enums.NotifyResponse;
import com.medusa.gruul.payment.common.enums.TradeStatus;
import com.medusa.gruul.payment.common.helper.PayHelper;
import com.medusa.gruul.payment.service.MultiPayOrderService;
import com.medusa.gruul.payment.service.MultiPayRefundService;
import com.medusa.gruul.payment.service.MultiPayVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 微信支付回调处理器
 * @author 张治保
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WxPayMessageHandler implements PayMessageHandler<WxPayMessage, WxPayService> {

    private final MultiPayVerifyService multiPayVerifyService;
    private final MultiPayOrderService orderPaymentService;
    private final MultiPayRefundService multiPayRefundService;
    @Override
    public PayOutMessage handle(WxPayMessage payMessage, Map<String, Object> context, WxPayService payService) throws PayErrorException {
        log.info("::微信支付::notify::{}", Jackson.writeValueAsString(payMessage));
        Map<String, Object> payMessageDetail = payMessage.getPayMessage();
        Object refundStatus = payMessageDetail.get(WxConst.Refund.REFUND_STATUS);
        String outTradeNo = payMessage.getOutTradeNo();
        /*
         * 退款通知
         */
        if (refundStatus != null){
            log.info("::微信支付::notify::{}","退款回调");
            multiPayRefundService.refundNotify(PayType.WECHAT,payMessage);
            return payService.getPayOutMessage(NotifyResponse.WECHAT_SUCCESS.getCode(), NotifyResponse.WECHAT_SUCCESS.getMsg());
        }
        /*
         * 支付结果通知
         */
        log.info("::微信支付::notify::{}","支付回调");
        TradeStatus tradeStatus = multiPayVerifyService.verifyPayStatus(PayType.WECHAT, outTradeNo);
        log.info("::微信支付::notify::交易状态:{}",tradeStatus);
        if ( TradeStatus.SUCCESS ==  tradeStatus){
            orderPaymentService.success(payMessage);
        }
        INotifyResponse response = PayHelper.notifyResponse(tradeStatus, PayType.WECHAT);
        log.info("::::::支付回调完毕::::::");
        return payService.getPayOutMessage(response.getCode(), response.getMsg());
    }

}
